<?php

/**
 * import actions.
 *
 * @package    ppstool
 * @subpackage import
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class importActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		//$this->form = new UploadForm();
		$this->getUser()->setAttribute('progress', 10);
	}

	public function executeUpload(sfWebRequest $request)
	{
		$mimetype = $_FILES['filename']['type'];
		$this->filename = $_FILES['filename']['name'];

		if (strlen($_FILES['filename']['tmp_name'])>0) {
			if (strpos($mimetype, 'xml')== false ) {
				$this->getUser()->setFlash('error', "File is not a XML File", false);
				$this->setTemplate('index');
			}
			else {

				//echo $_FILES['filename']['tmp_name'];
				$handle = fopen($_FILES['filename']['tmp_name'], 'r');
				$xmlstring = "";
				$this->linecounter = 1;
				while ($line = fgets($handle, 1000)) {
					//if ($linecounter > 4) {
						$xmlstring .=$line;
						//}
						$this->linecounter ++;
					}
					fclose($handle);
					//echo $n;
					$xmlstring = substr($xmlstring, strpos($xmlstring, '<?xml'));
					//echo $xmlstring = $n;

					$xml = simplexml_load_string($xmlstring);
					//$xml = simplexml_load_file($_FILES['filename']['tmp_name']);
					$this->getUser()->setAttribute('xmlin', $xml->asXML());

					$this->getUser()->setAttribute('progress', 20);
					$this->getUser()->setFlash('notice', "File Upload ok", false);
				}
			}
			else {
				$this->getUser()->setFlash('error', "No File given", false);
				$this->setTemplate('index');
			}
		}
	}
